/*
 * Flashlib - QB9 common Flash library
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.qb9.flashlib.tasks
{
	public class TimeBender extends TaskContainer
	{
		protected var _factor:Number;
		protected var accumulator:Number;
		
		public function TimeBender(factor:Number = 1)
		{
			super(true);
			
			_factor = factor;
			accumulator = 0;
		}
		
		public function get factor():Number
		{
			return _factor;
		}
		
		public function set factor(v:Number):void
		{
			_factor = v;
		}
		
		// TODO: Allow the time to run backwards!
		public override function update(milliseconds:uint):void
		{
			accumulator += milliseconds * factor;
			// The floor is unnecesary but self explanatory 
			var elapsed:uint = Math.floor(accumulator);
			accumulator -= elapsed;

			if (elapsed)
				super.update(elapsed);
		}
		
		/** @inheritDoc */
		public override function clone():ITask
		{
			var t:TimeBender = new TimeBender(factor);
			t.subtasks = cloneSubtasks();
			return t;
		}
		
	}
}